<?php
/**
 * Created by PhpStorm.
 * User: 刘红波 <liuhb@cqjcdflzxsws.com>
 * Date: 2018/8/13
 * Time: 9:50
 */

namespace app\sd\admin;


use app\admin\controller\Admin;
use app\sd\model\WeBadWords as MallBadWords;
use app\common\builder\ZBuilder;
use think\Db;

class BadWords extends Admin
{
    public function add()
    {
        if ($this->request->isGet()){
            return ZBuilder::make('form')
                ->addFormItem('text','words','违禁词')
                ->fetch();
        }
        if ($this->request->isPost()){
            $data = $this->request->post();
            $res = $this->validate($data,['words'=>'require|unique:we_bad_words,words'],['words.require'=>'违禁词汇不能为空']);
            if (true!== $res){
                $this->error($res);
            }else{
              $res =  \app\api\model\BadWords::create($data);
              if ($res->id){
                  $this->success('新增违禁词成功','badWords');
              }else{
                  $this->error('新增违禁词失败');
              }
            }
        }
    }

    public function badWords() {
        $list = MallBadWords::where('is_delete', 0)->field('id,words')->paginate('50');
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->fetch();
    }

    /**
     * 删除单个违禁词
     */
    public function delete_bad_words() {
        $id = input('id');
        $word = MallBadWords::get($id)->delete();
        if ($word) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 删除所有违禁词
     */
    public function delete_all_bad_words() {
        $res = Db::query('DELETE FROM `sd_we_bad_words`');
        if ($res) {
            $this->success('删除失败');
        } else {
            $this->error('删除成功,请从新导入');
        }
    }


    /**
     * 上传违禁词
     *
     * @return mixed
     * @throws \think\Exception
     */
    public function uploadBadWords() {
        if ($this->request->isGet()) {
            return ZBuilder::make('form')
                ->addFormItem('file', 'file', '上传文件')
                ->fetch();
        }
        if ($this->request->isPost()) {
            $file_path = $this->request->post('file');

            $path = str_replace(DS.DS,DS,ROOT_PATH.'public'.DS.get_file_path($file_path));
            $res = plugin_action('Excel', 'Excel', 'import', [$path, 'we_bad_words', ['words' => 'words'], 0, '', 'words']);
            if ($res['error'] == 0) {
                $this->success($res['message'],'badWords');
            } else {
                $this->error($res['message']);
            }

        }
    }

    /**
     * 下载违禁词
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function downloadBadWords() {
        $data_list = collection(MallBadWords::field('words')->select())->toArray();
        $cellName = [
            ['words', 'auto', 'words'],
        ];
        plugin_action('Excel/Excel/export', ['违禁词列表', $cellName, $data_list]);
    }
}